package com.yunwaikeji.gy_tool_library.adapter.recycler;

import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;
import com.yunwaikeji.gy_tool_library.view.GYImageView;
import com.yunwaikeji.gy_tool_library.view.GYTextView;

public class GYViewHolder extends RecyclerView.ViewHolder {
    private final SparseArray<View> viewSparseArray;
    private GYRecyclerAdapter<?, ?> gyRecyclerAdapter;

    public GYViewHolder(@NonNull View itemView) {
        super(itemView);
        viewSparseArray = new SparseArray<>();
    }

    public GYViewHolder setText(int viewId, Object text) {
        TextView textView = getView(viewId);
        if (text == null) {
            textView.setText("");
        } else {
            textView.setText(String.valueOf(text));
        }
        return this;
    }

    public GYViewHolder setTextColor(int viewId, int color) {
        TextView textView = getView(viewId);
        textView.setTextColor(color);
        return this;
    }

    public GYViewHolder setTextSizeSp(int viewId, int sp) {
        TextView textView = getView(viewId);
        textView.setTextSize(sp);
        return this;
    }

    public GYViewHolder setTextSizePx(int viewId, int px) {
        return setTextSizeSp(viewId, GYDisplayUtils.px2sp(itemView.getContext(), px));
    }

    public GYViewHolder setImage(int viewId, Drawable drawable) {
        View view = getView(viewId);
        if (view instanceof GYImageView) {
            GYImageView imageView = (GYImageView) view;
            imageView.getImageBuilder().setImageDrawable(drawable);
        } else if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            imageView.setImageDrawable(drawable);
        }
        return this;
    }

    public GYViewHolder setImage(int viewId, int resId) {
        return setImage(viewId, getView(viewId).getContext().getResources().getDrawable(resId));
    }

    public GYViewHolder setVisible(int viewId, Boolean visible) {
        getView(viewId).setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
        return this;
    }

    public GYViewHolder setGone(int viewId, Boolean visible) {
        getView(viewId).setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public <V extends View> V getView(@IdRes int viewId) {
        View view = viewSparseArray.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            viewSparseArray.put(viewId, view);
        }
        return (V) view;
    }

    public GYViewHolder setRecyclerAdapter(GYRecyclerAdapter gyRecyclerAdapter) {
        this.gyRecyclerAdapter = gyRecyclerAdapter;
        return this;
    }

    public final int getItemPosition() {
        return getAdapterPosition() - gyRecyclerAdapter.getHeaderLayoutCount();
    }
}
